package com.request; /**
 * @author wzm
 * @create 2023-04-25 15:38
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/request02")
public class Request02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      //调用doPost处理
        doPost(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求编码为UTF-8
        request.setCharacterEncoding("UTF-8");


        //通用的获取请求参数
        //1.获取所有参数的map集合
        Map<String, String[]> map = request.getParameterMap();
        //循环
      for(String key : map.keySet()){
          //获取map的key  username password hobby
          System.out.println("key:" + key);
          //根据map的key获取value
          String[] strings = map.get(key);
          //将值遍历出来
          for(String value : strings){
              System.out.println("value = " + value);
          }
          System.out.println("-------------");
      }

        //2.根据参数的名称获取值的数组
        System.out.println("**********************************");
        String[] hobbies = request.getParameterValues("hobby");//获取爱好的多个属性值作为数组
        for (String hobby:hobbies){
            System.out.println("hobby = " + hobby);
        }

        //3.根据参数名称获取参数值

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("password = " + password);
        System.out.println("username = " + username);


        //跳转到某个Servlet或某个页面？


    }
}
